// 题目描述有问题
// 应该是: i和j的差的绝对值的最大值<=k

package main

import "fmt"

func containsNearbyDuplicate(nums []int, k int) bool {
	hm := make(map[int][]int)
	for index, val := range nums {
		hm[val] = append(hm[val], index)
	}
	for _, hmval := range hm {
		if len(hmval) == 2 {
			sub := hmval[1] - hmval[0]
			if sub <= k {
				return true
			}
		} else if len(hmval) > 2 {
			for i := len(hmval) - 1; i > 0; i-- {
				if hmval[i]-hmval[i-1] <= k {
					return true
				}
			}
		}

	}
	return false
}

func betterContainsNearbyDuplicate(nums []int, k int) bool {
	if len(nums) <= 1 {
		return false
	}

	nmap := make(map[int]int)
	for index, val := range nums {
		fmt.Println("-------------------------------")
		fmt.Println("nmap: ", nmap)
		fmt.Printf("index: %d, value: %d\n", index, val)
		if mi, ok := nmap[val]; ok {
			if index-mi <= k {
				return true
			}
		}
		nmap[val] = index
	}
	return false
}
